package Server;

import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
import GameState.GameState;
import Server.Requests.MovementRequest;
import Server.Requests.ShootingRequest;
import Server.Requests.TurningRequest;





public class GameServer extends UnicastRemoteObject implements GameServerInterface, Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GameState mTheGame;
	private Map<Player, Request> mThePlayers;

	private boolean mRunServer = true;

	public GameServer() throws RemoteException, IOException{

		mTheGame = new GameState();
		mThePlayers = new HashMap<Player, Request>();

	}


	public void run(){

		//try {

		do{

			try {
				Thread.sleep(10L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			update(); // Update the gamestate

			getConnections();



		} while  (mRunServer);

		//} catch (RemoteException e) {
		//	e.printStackTrace();
		//}


	}

	private void getConnections(){



	}

	@Override
	public void AddPlayer(Player p) throws RemoteException {

		mThePlayers.put(p, new Request());
		//mThePlayers.add(p);
		mTheGame.addPlayer(p);
	}


	@Override
	public void RemovePlayer(Player p) throws RemoteException {
		mThePlayers.remove(p);

		mTheGame.removePlayer(p);

		if(mThePlayers.isEmpty()) //Closes the server fi ther are no more in the game
			mRunServer = false;

	}


	@Override
	public GameState getGameState() throws RemoteException {
		// TODO Auto-generated method stub
		return mTheGame;
	}


	@Override
	public int getNumberOfPlayers() throws RemoteException {
		// TODO Auto-generated method stub
		return mThePlayers.size();
	}


	@Override
	public int getNumberOfShips() throws RemoteException {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public void playerRequest(Request playerRequest, Player player) throws RemoteException {


		Request playerR = mThePlayers.get(player);

		switch (playerRequest.getMovementRequest())
		{
		case NONE:
			//Do nothing
			break;

		case SPEED_UP:

			//Change the request going to the server to speed up
			playerR.setMovementRequest(MovementRequest.SPEED_UP);
			break;

		}

		switch (playerRequest.getShootingRequest())
		{
		case NONE:
			//Do nothing
			break;

		case SHOOT_LEFT:

			playerR.setShootingRequest(ShootingRequest.SHOOT_LEFT);
			break;

		case SHOOT_RIGHT:
			playerR.setShootingRequest(ShootingRequest.SHOOT_RIGHT);
				break;


		}

		switch (playerRequest.getTurningRequest())
		{

		case NONE:
			//Do nothing
			break;
			
		case TURN_RIGHT:
			playerR.setTurningRequest(TurningRequest.TURN_RIGHT);
			break;
			
		case TURN_LEFT:
			playerR.setTurningRequest(TurningRequest.TURN_LEFT);
			break;


		}


	}


	private void update(){


	}
}


